import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'el-icon-s-home' }
    }]
  },
  {
    path: '/product',
    component: Layout,
    meta: { title: '商品管理', icon: 'el-icon-s-goods' },
    children: [
      {
        path: '/spu/list',
        name: 'Spu',
        component: () => import('@/views/product/spu/index'),
        meta: { title: 'SPU台账', icon: 'el-icon-present' }
      },
      {
        path: '/spu/step1',
        name: 'SpuStep1',
        hidden: true,
        component: () => import('@/views/product/spu/step1'),
        meta: { title: '类别选择', icon: 'el-icon-el-icon-receiving' }
      },
      {
        path: '/spu/step2',
        name: 'SpuStep2',
        hidden: true,
        component: () => import('@/views/product/spu/step2'),
        meta: { title: '添加图片', icon: 'el-icon-present' }
      },
      {
        path: '/spu/step3',
        name: 'SpuStep3',
        hidden: true,
        component: () => import('@/views/product/spu/step3'),
        meta: { title: '添加详情', icon: 'el-icon-present' }
      },
      {
        path: '/spu/step4',
        name: 'SpuStep4',
        hidden: true,
        component: () => import('@/views/product/spu/step4'),
        meta: { title: '添加属性', icon: 'el-icon-present' }
      },
      {
        path: '/album',
        name: 'Album',
        component: () => import('@/views/product/album/index'),
        meta: { title: '相册管理', icon: 'el-icon-picture' }
      },
      {
        path: '/category/list/:parentId',
        name: 'Category',
        component: () => import('@/views/product/category/index'),
        meta: { title: '商品类别', icon: 'el-icon-receiving' }
      },
      {
        path: '/category/add',
        name: 'AddCategory',
        hidden: true,
        component: () => import('@/views/product/category/add'),
        meta: { title: '新增类别', icon: 'form' }
      },
      {
        path: '/category/edit/:id', // 动态路由
        name: 'EditCategory',
        hidden: true,
        component: () => import('@/views/product/category/edit'),
        meta: { title: '编辑类别', icon: 'form' }
      },
      {
        path: '/attribute',
        name: 'Attribute',
        component: () => import('@/views/product/attribute/index'),
        meta: { title: '属性模板', icon: 'el-icon-s-order' }
      },
      {
        path: '/brand',
        name: 'Brand',
        component: () => import('@/views/product/brand/index'),
        meta: { title: '品牌管理', icon: 'el-icon-collection' }
      },
      {
        path: '/brand/add',
        name: 'AddBrand',
        hidden: true,
        component: () => import('@/views/product/brand/add'),
        meta: { title: '新增品牌', icon: 'form' }
      },
      {
        path: '/brand/edit/:id', // 动态路由
        name: 'EditBrand',
        hidden: true,
        component: () => import('@/views/product/brand/edit'),
        meta: { title: '编辑品牌', icon: 'form' }
      },
      {
        path: '/tag',
        name: 'Tag',
        component: () => import('@/views/product/tag/index'),
        meta: { title: '标签管理', icon: 'el-icon-collection-tag' }
      }
    ]
  },
  {
    path: '/sku',
    name: 'Sku',
    component: Layout,
    meta: { title: '库存管理', icon: 'el-icon-s-claim' },
    children: [
      {
        path: '/sku/list',
        name: 'SkuList',
        component: () => import('@/views/sku/index'),
        meta: { title: '库存列表', icon: 'el-icon-s-order' }
      }
    ]
  },
  {
    // 订单管理
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', icon: 'el-icon-s-order' }
  },
  {
    // 营销管理
    path: '/promotion',
    component: Layout,
    meta: { title: '营销管理', icon: 'el-icon-s-marketing' }
  },
  {
    // 系统管理
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [
      {
        path: '/system/admin',
        name: 'Admin',
        component: () => import('@/views/system/admin/index'),
        meta: { title: '系统用户', icon: 'el-icon-user-solid' }
      },
      {
        path: '/system/role',
        name: 'Role',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色管理', icon: 'el-icon-circle-check' }
      },
      {
        path: '/system/assignment',
        name: 'Assignment',
        hidden: true,
        component: () => import('@/views/system/role/assignment'),
        meta: { title: '分配权限', icon: 'el-icon-s-order' }
      },
      {
        path: '/system/permission',
        name: 'Permission',
        component: () => import('@/views/system/permission/index'),
        meta: { title: '权限管理', icon: 'el-icon-s-check' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
